package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.CustomException;
import com.itheima.domain.Category;

import com.itheima.domain.Dish;
import com.itheima.domain.Setmeal;
import com.itheima.mapper.CategoryMapper;

import com.itheima.service.CategoryService;

import com.itheima.service.DishService;
import com.itheima.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    @Override
    public void delete(Long id) {
        LambdaQueryWrapper<Dish> dishWrapper = new LambdaQueryWrapper<>();
        dishWrapper.eq(Dish::getCategoryId, id);
//        int res = dishService.count(dishWrapper);
        List<Dish> dishList = dishService.list(dishWrapper);
        //存在相等的菜品
        log.info("dishList:{}", dishList);
        if (dishList != null && dishList.size() > 0) {
            throw new CustomException("当前分类下关联了菜品，不能删除");
        }
        LambdaQueryWrapper<Setmeal> setmealWrapper = new LambdaQueryWrapper<>();
        setmealWrapper.eq(Setmeal::getCategoryId, id);
//        int res2 = setmealService.count(setmealWrapper);
        List<Setmeal> setmealList = setmealService.list(setmealWrapper);
        //存在相等的菜品
        log.info("setmealList:{}", setmealList);
        if (setmealList != null && setmealList.size() > 0) {
            throw new CustomException("当前分类下关联了套餐，不能删除");
        }
        super.removeById(id);
    }
}


